package com.daoImpl;

import java.util.List;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.dao.*;
import com.entity.Device;
import com.entity.Parameter;
import com.entity.Substation;

public class SubstationDaoImpl extends HibernateDaoSupport implements SubstationDao {

	@Override
	public List<Substation> findAllSubstation() {
		List list = (List<Substation>) this.getHibernateTemplate().find("from Substation");
		return list;
	}

	@Override
	public Substation findSubstationByID(int substationID) {
		return (Substation) this.getHibernateTemplate().get(Substation.class, substationID);
	}

	@Override
	public Substation findSubstatinByNo(String substationNo) {
		List list = (List<Substation>) this.getHibernateTemplate().find("from Substation a where a.substationNo=?",new Object[]{substationNo});
		Substation substation = (Substation)list.get(0);
		return substation;
	}

	@Override
	public boolean addSubstation(Substation substation) {
		try{
            this.getHibernateTemplate().clear();
            this.getHibernateTemplate().save(substation);
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
	}

	@Override
	public boolean deleteSubstation(Substation substation) {
		try{
            this.getHibernateTemplate().delete(substation);
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
	}

	@Override
	public boolean updateSubstation(Substation substation) {
		try{
            this.getHibernateTemplate().clear();
            this.getHibernateTemplate().update(substation);
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
	}
	
	@Override
	public List<Substation> findAuditingSubstation(){
		List list = (List<Substation>) this.getHibernateTemplate().find("from Substation");
		return list;
	}
	
	@Override
	public boolean checkSubstationNoAvailable(String substationNo) {
		List list = (List<Substation>) this.getHibernateTemplate().find("from Substation s where s.substationNo=?",new Object[]{substationNo});
        if(list.size() != 0)
        	return true;
        else 
        	return false;
	}
	
	@Override
	public List<Substation> substationDetails(String substationNo){
		List list = (List<Substation>) this.getHibernateTemplate().find("from Substation s where s.substationNo='"+ substationNo +"'");
		return list;
	}

	@Override
	public List<Substation> findAllSubstationByAvailable() {
		List list = (List<Substation>) this.getHibernateTemplate().find("from Substation where substationStatus != 2");
		return list;
	}

}
